package com.glhtgyxy.biyesheji.Service;

import com.glhtgyxy.biyesheji.Mapper.clientMapper;
import com.glhtgyxy.biyesheji.Pojo.evaluate;
import com.glhtgyxy.biyesheji.Pojo.house;
import com.glhtgyxy.biyesheji.Pojo.merchant;
import com.glhtgyxy.biyesheji.Pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class clientService {

    @Autowired
    clientMapper mapper;

    public ArrayList<house> findHouse(
            String addr,
            String price,
            String bedroom,
            String search
    ){
        HashMap<String,String> map = new HashMap<>();
        if (!addr.equals("全部"))
        {
            map.put("addr",addr);
        }
        if (!price.equals("全部"))
        {
            if (price.contains("-"))
            {
                String[] split = price.split("-");
                map.put("pricelow",split[0]);
                map.put("priceheigh",split[1]);
            }else if (price.contains("以上"))
            {
                String[] split = price.split("以");
                map.put("pricelow",split[0]);
                map.put("priceheigh","99999");
            }else if (price.contains("以下"))
            {
                String[] split = price.split("以");
                map.put("pricelow","0");
                map.put("priceheigh",split[0]);
            }
        }
        if (!bedroom.equals("全部"))
        {
            map.put("bedroom",bedroom);
        }
        if (!search.equals("全部"))
        {
            map.put("search",search);
        }

        return  mapper.findHouse(map);
    }

    public ArrayList<house> findHouseById(
            int id
    ){
        return  mapper.findHouseById(id);
    }

    public ArrayList<merchant> houseIdFindMaser(
            int id
    ){
        int i = mapper.houseIdFindMaser(id);
        ArrayList<merchant> merchants = mapper.masterIdFindMerchant(i);
        return  merchants;
    }

    public ArrayList<evaluate> merchantIdFindEvaluate(
            int id
    ){
        return mapper.merchantIdFindEvaluate(id);
    }

    public boolean tijiaodingdan(
            int houseid,
            int userid,
            String checkInDate,
            String checkOutDate
    ){
        return mapper.tijiaodingdan(houseid,userid,checkInDate,checkOutDate)>0?true:false;
    }



    public ArrayList<user> evaFindUser(
            int id
    ){
        int[] ints = mapper.evaFindUser(id);
        ArrayList<user> users = new ArrayList<>();
        for (int i = 0; i < ints.length; i++) {
            user finduser = mapper.finduser(ints[i]);
            users.add(finduser);
        }
        return users;
    }
}
